package org.iabako.client.ui.form.business.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.tools.Messages;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.layout.widget.customField.CustomFieldsInstance;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.business.Client;
import org.iabako.shared.entity.business.CustomField;
import org.iabako.shared.entity.business.CustomFieldValue;
import org.iabako.shared.entity.enumeration.IabakoPackage;
import org.iabako.shared.iservice.business.ClientServiceAsync;
import org.iabako.shared.tools.GenericTools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jose on 17/09/14.
 */
public class ClientCustomFieldsForm extends AbstractForm {


    interface MyUiBinder extends UiBinder<HTMLPanel, ClientCustomFieldsForm> {
    }

    protected MyUiBinder binderClientForm = GWT.create(MyUiBinder.class);

    protected final ClientServiceAsync clientServiceAsync = ClientServiceAsync.Util.getInstance();

    @UiField
    CustomFieldsInstance customFieldsPanel;
    @UiField
    HTMLPanel noCustomFieldsPanel;
    @UiField
    HTMLPanel waitCustomFieldsPanel;
    @UiField
    LabelText noCustomFieldLabel;
    @UiField
    LabelText customFieldForbiddenLabel;
    @UiField
    LabelText someIgnoredCustomFieldsNote;
    @UiField
    HTMLPanel someIgnoredCustomFieldsNotePanel;

    public ClientCustomFieldsForm() {
        initWidget(binderClientForm.createAndBindUi(this));
    }

    protected void initialize(Client client) {

        this.entity = client;

        clientServiceAsync.getCustomFieldsValue(client, new IabakoAsyncCallback<List<CustomFieldValue>>() {
            public void onSuccess(List<CustomFieldValue> result) {

                IabakoPackage iabakoPackage = AuthenticateUserProvider.getUser().getUserCredit().getIabakoPackage();
                IabakoPackage nextPackage = iabakoPackage.isStarter() ? IabakoPackage.proMonthly : IabakoPackage.enterpriseMonthly;

                List<CustomField> enterpriseCustomFields = AuthenticateUserProvider.getUser().getEnterprise().getCustomFields();
                int ignoredCustomField = enterpriseCustomFields.size() - AuthenticateUserProvider.getUser().getUserCredit().getIabakoPackage().getCustomFieldLimit();
                boolean allCustomFieldsIgnored = ignoredCustomField == enterpriseCustomFields.size();

                List<CustomFieldValue> valuesToShow = new ArrayList<CustomFieldValue>();
                List<String> ignoredCustomFields = new ArrayList<String>();

                if (ignoredCustomField <= 0) {
                    valuesToShow.addAll(result);

                } else {
                    for (int i = ignoredCustomField; i > 0; i--) {
                        ignoredCustomFields.add(enterpriseCustomFields.get(enterpriseCustomFields.size() - i).getTitle());
                    }
                    for (CustomFieldValue value : result) {
                        if (result.indexOf(value) < result.size() - ignoredCustomField) {
                            valuesToShow.add(value);
                        }
                    }
                }

                customFieldsPanel.setCustomFieldValues(valuesToShow);

                waitCustomFieldsPanel.setVisible(false);

                if (enterpriseCustomFields.size() == 0) {
                    noCustomFieldsPanel.setVisible(true);

                } else if (allCustomFieldsIgnored) {
                    noCustomFieldsPanel.setVisible(true);
                    noCustomFieldLabel.setVisible(false);
                    customFieldForbiddenLabel.setLabel(Messages.getLabel("persofield_label_ignored", iabakoPackage.getShortLabel(), GenericTools.listAsString(ignoredCustomFields), nextPackage.getShortLabel()));
                    customFieldForbiddenLabel.setVisible(true);

                } else {
                    customFieldsPanel.setVisible(true);
                    if (!ignoredCustomFields.isEmpty()) {
                        someIgnoredCustomFieldsNote.setLabel(Messages.getLabel("persofield_label_ignored", iabakoPackage.getShortLabel(), GenericTools.listAsString(ignoredCustomFields), nextPackage.getShortLabel()));
                        someIgnoredCustomFieldsNotePanel.setVisible(true);
                    }
                }

                super.onSuccess(result);
            }
        });
    }

    public void setCustomFieldsValueToClient(Client client) {
        customFieldsPanel.setInputValuesToCustomFieldValues();
        for (CustomFieldValue value : customFieldsPanel.getInputValues().keySet()) {
            client.getCustomFieldValues().add(value);
        }
    }


    @Override
    protected List<IField> getMandatoryFields() {
        return null;
    }

    @Override
    protected AbstractEntity getEntity() {
        return null;
    }

    @Override
    public boolean validate() {
        return false;
    }
}
